<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>

</body>
<script>
    // 剧团演出剧目表
    let show_list = {
        "hamlet": {
            "name": "Hamlet",
            "type": "tragedy"
        },
        "as-like": {
            "name": "As You Like It",
            "type": "comedy"
        },
        "othello": {
            "name": "Othello",
            "type": "tragedy"
        }
    };
    // 剧团开出的账单
    let money_order = [{
        "customer": "BigCo",
        "performances": [{
            "playID": "hamlet",
            "audience": 55
        }, {
            "playID": "as-like",
            "audience": 35
        }, {
            "playID": "othello",
            "audience": 40
        }]
    }];
    var sky = statement(money_order[0], show_list);
    // 打印账单详情的函数
    // invoice为剧团开出的账单，plays为剧团演出剧目表
    function statement(invoice, plays) {
        let totalAmount = 0;
        let volumeCredits = 0;
        // Statement结算单的意思，后面跟着用户的名字
        let result = `Statement for ${invoice.customer}\n`;

        const format = new Intl.NumberFormat("en-US", {
            style: "currency",
            currency: "USD",
            minimumFractionDigits: 2
        }).format;
        // 遍历用户看过的所有剧目
        for (let perf of invoice.performances) {
            debugger
            // paly为剧目变对应的对象，比如"hamlet": {
            //     "name": "Hamlet",
            //     "type": "tragedy"
            // },
            const play = plays[perf.playID];
            let thisAmount = 0;

            switch (play.type) {
                // 悲剧
                case "tragedy":
                    thisAmount = 40000;
                    if (perf.audience > 30) {
                        thisAmount += 1000 * (perf.audience - 30);
                    }
                    break;
                    // 喜剧
                case "comedy":
                    thisAmount = 30000;
                    if (perf.audience > 20) {
                        thisAmount += 10000 + 500 * (perf.audience - 20);
                    }
                    thisAmount += 300 * perf.audience;
                    break;
                    // 默认
                default:
                    throw new Error(`unknown type: ${play.type}`);
            }

            // add volume credits
            volumeCredits += Math.max(perf.audience - 30, 0);
            // add extra credit for every ten comedy attendees
            if ("comedy" === play.type) volumeCredits += Math.floor(perf.audience / 5);

            // print line for this order
            debugger
            result += ` ${play.name}: ${format(thisAmount/100)} (${perf.audience} seats)\n`;
            totalAmount += thisAmount;
        }
        // Amount数量，owed欠我的【totalAmount，总数量意思】
        result += `Amount owed is ${format(totalAmount/100)}\n`;
        // earned赚得，credits信用，积分的意思，volume，容量，体积的意思
        result += `You earned ${volumeCredits} credits\n`;

        return result;
    }
    console.log(sky);
</script>

</html>